﻿using System.Data;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using StoreAPI.Models;
using StoreAPI.Services;

namespace StoreAPI.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UserController : ControllerBase
    {
        private readonly IUserService _userService;
        public UserController(IUserService userService)
        {
            _userService = userService;
        }
        //登录
        [HttpPost("Login")]
        public ActionResult Login([FromBody] User model)
        {
            var user = _userService.Login(model);
            if (user != null)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "登录成功",
                    User = user
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "登录失败"
            });

        }
        //得到最大ID
        [HttpPost("GetMaxId")]
        public ActionResult GetMaxId()
        {
            var maxId = _userService.GetMaxId();
            if (maxId != 0)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    MaxId = maxId
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });
        }
        //是否存在该记录
        [HttpPost("IsExist")]
        public ActionResult Exists(int userId)
        { 
         if (_userService.Exists(userId))
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "存在"
                }); 
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "不存在"
            });
        }

        //添加
        [HttpPost("AddUser")]
        public ActionResult Add([FromBody] User model)
        {
            if (_userService.Add(model))
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "添加成功"
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "添加失败"
            });
        }

        //更新
        [HttpPost("UpdateUser")]
        public ActionResult Update([FromBody] User model)
        {
            if (_userService.Update(model))
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "更新成功"
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "更新失败"
            });
        }
        //删除
        [HttpPost("DeleteUser")]
        public ActionResult Delete([FromBody] int userId)
        {
            if (_userService.Delete(userId))
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "删除成功"
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "删除失败"
            });
        }
        //批量删除数据
        [HttpPost("DeleteUsers")]
        public ActionResult DeleteUsers([FromBody] int[] userIds)
        {
            if (_userService.DeleteList(userIds))
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "删除成功"
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "删除失败"
            });
        }
        //得到一个对象实体
        [HttpPost("GetUser")]
        public ActionResult GetUser([FromBody] int userId)
        {
            var user = _userService.GetModel(userId);
            if (user != null)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    Data = user
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });
        }
        //查询一行数据
        [HttpPost("DataRowToUser")]
        public ActionResult DataRowToUser([FromBody] DataRow dr)
        {
            var user = _userService.DataRowToModel(dr);
            if (user != null)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "转换成功",
                    Data = user
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "转换失败"
            });
        }


        //获取全部用户
        [HttpPost("GetListAllUser")]
        public ActionResult GetListAllUser([FromBody] Expressionable<User> exp)
        {
            var user = _userService.GetListAll(exp);
            if (user != null)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    Data = user
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });
        }
        //获得前几行数据
        [HttpPost("GetListUser")]
        public ActionResult GetListUser([FromBody] Expressionable<User> exp, int Top)
        {
            var user = _userService.GetList(exp, Top);
            if (user != null)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    Data = user
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });
        }
        //获取记录总数
        [HttpPost("GetRecordCountUser")]
        public ActionResult GetRecordCountUser([FromBody] Expressionable<User> exp)
        {
          if (_userService.GetRecordCount(exp) > 0)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    Count = _userService.GetRecordCount(exp)
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });
        
        }


        //分页
        [HttpPost("GetUserPage")]
        public ActionResult GetUserPage([FromBody] Expressionable<User> exp, int pageIndex = 1, int pageSize = 10)
        {
            var totalPage = 0;
            var user = _userService.GetListByPage(exp, out totalPage, pageIndex, pageSize);
            if (user != null)
            {
                return Ok(new
                {
                    Code = 200,
                    Msg = "获取成功",
                    Data = user,
                    TotalPage = totalPage
                });
            }

            return Ok(new
            {
                Code = 10000,
                Msg = "获取失败"
            });
        }
    }
}
